package at.fhj.swd.pse.data;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import at.fhj.swd.pse.entities.News;

/**
 * 
 * @author Martin Steinbauer
 *
 */
@Stateless
public class NewsDAOImpl implements NewsDAO {

	@PersistenceContext
	private EntityManager em;
	
	public NewsDAOImpl() {
	}
	
	public NewsDAOImpl(EntityManager em) {
		this.em = em;
	}
	
	@Override
	public News insert(News entity) {
		return em.merge(entity);
	}

	@Override
	public News get(int id) {
		return em.find(News.class, id);
	}

	@Override
	public List<News> getAll() {
		return em.createQuery("select n from News n", News.class).getResultList();
	}

	@Override
	public void remove(News entity) {
		em.remove(entity);
	}

	@Override
	public News update(News entity) {
		return em.merge(entity);
	}

	@Override
	public List<News> getAllOrderedByCreation() {
		return em.createQuery("select n from News n order by n.timestamp desc", News.class).getResultList();
	}

}
